package cn.caplike.demo.rabbitmq.api.consumer;

import cn.caplike.demo.rabbitmq.api.Application;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author LiKe
 * @version 1.0.0
 * @date 2020-02-19 12:18
 */
public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost(Application.RABBITMQ_HOST);
        connectionFactory.setPort(Application.RABBITMQ_PORT);
        connectionFactory.setVirtualHost("/");

        // 通过链接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        // 通过连接创建一个 Channel
        Channel channel = connection.createChannel();

        final String exchangeName = "test_consumer_exchange";
        final String routingKey = "consumer.save";

        // 发送消息
        final String msg = "Hello RabbitMQ Send consumer message!";
        channel.basicPublish(exchangeName, routingKey, false, null, msg.getBytes());
        channel.basicPublish(exchangeName, routingKey, false, null, msg.getBytes());

        channel.close();
        connection.close();
    }
}
